package com.changgou.search.controller;

import com.changgou.entity.Page;
import com.changgou.search.pojo.SkuInfo;
import com.changgou.search.service.ESManagerService;
import com.changgou.search.service.SearchService;
import org.elasticsearch.search.SearchModule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

/**
 * 搜索查询表现层
 */

@Controller
@RequestMapping("/sku_search")
public class SearchController {
    @Autowired
    private ESManagerService esManagerService;

    @Autowired
    private SearchService searchService;

    /**
     * 对搜索入参带有特殊符合进行处理
     * 浏览器网址url中特殊字符转义编码：+ -> %2B
     * @param searchMap
     */
    public void handlerSearchMap(Map<String, String> searchMap) {
        if (null != searchMap) {
            Set<Map.Entry<String, String>> entries = searchMap.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                if (entry.getKey().startsWith("spec_")) {
                    searchMap.put(entry.getKey(), entry.getValue().replace("+", "%2B"));
                }
            }
        }
    }

    // 全文检索
    @GetMapping
    @ResponseBody
    public Map search(@RequestParam Map<String, String> paramMap) throws Exception {
        // 调用特殊符号处理功能
        handlerSearchMap(paramMap);
        Map search = searchService.search(paramMap);
        return search;
    }

    @GetMapping("/list")
    public String list(@RequestParam Map<String, String> paramMap, Model model) throws Exception {
        // 特殊符号处理
        this.handlerSearchMap(paramMap);

        // 获取查询结果
        Map resultMap = searchService.search(paramMap);
        // 查询结果数据
        model.addAttribute("result", resultMap);
        // 查询条件数据
        model.addAttribute("searchMap", paramMap);

        // 封装分页数据并返回
        Page<SkuInfo> page = new Page<SkuInfo>(
                Long.parseLong(resultMap.get("total").toString()),   // 总记录数
                Integer.parseInt(resultMap.get("pageNum").toString()),    // 当前页
                Page.pageSize    // 每页显示条数
        );
        model.addAttribute("page",page);


        // 拼装url
        StringBuilder url = new StringBuilder("/sku_search/list");
        if (paramMap != null && paramMap.size() > 0) {
            // 显示查询条件
            url.append("?");
            for (Object paramKey : paramMap.keySet()) {
                if (!"sortRule".equals(paramKey) && !"sortField".equals(paramKey) && !"pageNum".equals(paramKey)) {
                    url.append(paramKey).append("=").append(paramMap.get(paramKey)).append("&");
                }
            }
            // http://localhost:9009/sku_search/list?keywords=手机&spec_网络制式=4G
            String urlString = url.toString();
            // 去除路径上最后一个&
            urlString = urlString.substring(0, urlString.length() - 1);
            model.addAttribute("url", urlString);
        } else {
            model.addAttribute("url", url);
        }

        return "search";  // 跳转到search页面
    }

}

